package httputils;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.example.administrator.maizuofilm.bean.Hot;
import com.example.administrator.maizuofilm.bean.NotShow;
import com.example.administrator.maizuofilm.bean.Recommend;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by Administrator on 2016/7/12.
 */
public class HttpUtils {
    private static IRetrofitHttp http;

    private HttpUtils() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(UrlConfig.MAIZUOFILM).build();
        http = retrofit.create(IRetrofitHttp.class);
    }

    private static HttpUtils httpUtils;

    public static HttpUtils newInstance() {
        if (httpUtils == null) {
            httpUtils = new HttpUtils();
        }
        return httpUtils;
    }

    //        public static void getString( String First,String Last) {
//        HashMap<String, String> map = new HashMap<>();
////        Call<ResponseBody> call = http.getString(UrlConfig.NOTSHOW_FIRST + "12" + UrlConfig.NOTSHOW_LAST);
//        Call<ResponseBody> call = http.getString();
////        Call<ResponseBody> call = http.getString(First + "12" + Last);
//        call.enqueue(new Callback<ResponseBody>() {
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                try {
//                    String json = response.body().string();
//                    EventBus.getDefault().post(json);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    EventBus.getDefault().post(null);
//                }
//
//            }
//
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable t) {
//                EventBus.getDefault().post(null);
//            }
//        });
//    }
//    public static void getCityList( ) {
//        HashMap<String, String> map = new HashMap<>();
//        Call<ResponseBody> call = http.getString(UrlConfig.CITY_LIST);
//        call.enqueue(new Callback<ResponseBody>() {
//            @Override
//            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
//                try {
//                    String json = response.body().string();
//                    EventBus.getDefault().post(json);
//                } catch (IOException e) {
//                    e.printStackTrace();
//                    EventBus.getDefault().post(null);
//                }
//
//            }
//
//            @Override
//            public void onFailure(Call<ResponseBody> call, Throwable t) {
//                EventBus.getDefault().post(null);
//            }
//        });
//    }
    public static void getNotShow(String cityId,String index) {
        Map<String, String> map = new HashMap<>();
        map.put("index", index);
        map.put("count", "10");
        map.put("type", "2");
        map.put("channelID", "31");
        map.put("clientID", "31");
        map.put("revision", "4.6.5");
        Call<ResponseBody> call = http.getNotShow(cityId, map);

        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {

                    String json = response.body().string();
                    NotShow notShow = JSON.parseObject(json, NotShow.class);
                    EventBus.getDefault().post(notShow);
                } catch (IOException e) {
                    e.printStackTrace();
                    EventBus.getDefault().post(null);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                EventBus.getDefault().post(null);
            }
        });
    }

    public static void getRecommend() {
        Map<String, String> map = new HashMap<>();
        map.put("userId", "e72118d5c26c7edacc5905b00c2c30a2");
        map.put("sessionKey", "");
        map.put("type", "1,2");
        map.put("channelID", "31");
        map.put("clientID", "31");
        map.put("revision", "4.6.5");
        Call<ResponseBody> call = http.getRecommend();
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String json = response.body().string();
                    Recommend recommend = JSON.parseObject(json, Recommend.class);
                    EventBus.getDefault().post(recommend);
                } catch (IOException e) {
                    e.printStackTrace();
                    EventBus.getDefault().post(null);
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                EventBus.getDefault().post(null);
            }
        });
    }

    public static void getHot() {
        Map<String, String> map = new HashMap<>();
//index=0&count=10&type=1&channelID=31&clientID=31
        map.put("index", "0");
        map.put("count", "10");
        map.put("type", "1");
        map.put("channelID", "31");
        map.put("clientID", "31");
        Call<ResponseBody> call = http.getHot("12");
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    String json = response.body().string();
                    Hot hot = JSON.parseObject(json, Hot.class);
                    EventBus.getDefault().post(hot);
                } catch (IOException e) {
                    e.printStackTrace();
                }
//                EventBus.getDefault().post(null);
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                EventBus.getDefault().post(null);
            }
        });
    }
}
